
$('#J_login').on('click', function () {
  var account = $('[name=account]').val()
  var password = $('[name=password]').val()

  if(!account || !password) {
    return
  }
  var data = {
    account: account,
    password: password
  }
  openidlogin(data)
})

function openidlogin(data) {
  $.ajax({
    url: '/api/user/openidlogin',
    method: 'post',
    data: data,
    success: function (res) {
      if(res.status.code !== 0) {
        $('#J_tips').show().text('帐号或密码不正确')
      } else {
        var courseId = $.urlToObj(location.href).courseid
        if(courseId !== undefined) {
          location.assign('course.html?id='+courseId+'&modal=open')
          return
        }
        if(res.data.wechat_type){
          switch (res.data.wechat_type) {
            case 1:
              location.assign('/admin/index.html')
              break;
            case 2:
              if(res.data.url === 'schedule'){
                location.assign('/teacher/index_m_s.html#timetable')
                break;
              }
              location.assign('/teacher/index_m.html')
              break;
            case 3:
              if(res.data.url === 'schedule'){
                location.assign('/student/index_m_s.html#student_lessontimes')
                break;
              }
              location.assign('/student/index_m.html')
              break;
          }
        }else{
          var types = res.data.types
          if(types.length == 0) {
            $('#J_tips').show().text('没有身份')
            return
          }

          if(types.length > 1) {
            location.assign('role_m.html')
            return
          }

          switch (res.data.types[0]) {
            case 1:
              location.assign('/admin/index.html')
              break;
            case 2:
              if(res.data.url === 'schedule'){
                location.assign('/teacher/index_m_s.html#timetable')
                break;
              }
              location.assign('/teacher/index_m.html#personInfo')
              break;
            case 3:
              if(res.data.url === 'schedule'){
                location.assign('/student/index_m_s.html#student_lessontimes')
                break;
              }
              location.assign('/student/index_m.html#student_personal_center')
              break;
          }
        }


      }
    }
  })
}
